#include "MetaUnitVariable.h"

#include "BuildUnit.h"

namespace BroodWar
{
	namespace Addon
	{
		BWSAL::MetaUnit* ConvertMetaUnit(IMetaUnit^ unit)
		{
			MetaUnitVariable^ meta = dynamic_cast<MetaUnitVariable^>(unit);
			if(meta != nullptr)
				return (BWSAL::MetaUnit*)(meta->instance);
			BuildUnit^ build = dynamic_cast<BuildUnit^>(unit);
			if(build != nullptr)
				return (BWSAL::BuildUnit*)(build->instance);
			throw gcnew NotSupportedException("For now interface inheritors are not supported!");
		}

		MetaUnitVariable^ ConvertMetaUnitVariable(BWSAL::MetaUnitVariable* unit)
		{
			if(unit == NULL)
				return nullptr;
			return gcnew MetaUnitVariable(unit);
		}

		BWSAL::MetaUnitVariable* ConvertMetaUnitVariable(MetaUnitVariable^ unit)
		{
			if(unit == nullptr)
				return NULL;
			return unit->instance;
		}

		MetaUnitVariable::MetaUnitVariable(BWSAL::MetaUnitVariable* unit)
		{
			instance = unit;
			dispose = false;
		}
		
		MetaUnitVariable::~MetaUnitVariable()
		{
			if(dispose)
				delete instance;
		}
		
		MetaUnitVariable::!MetaUnitVariable()
		{
			if(dispose)
				delete instance;
		}
		
		MetaUnitVariable::MetaUnitVariable()
		{
			instance = new BWSAL::MetaUnitVariable();
			dispose = true;
		}

		BuildUnit^ MetaUnitVariable::BuilderUnit::get()
		{
			return ConvertBuildUnit(instance->getBuildUnit());
		}
		
		Build^ MetaUnitVariable::Type::get()
		{
			return ConvertBuild(instance->getType());
		}
		
		void MetaUnitVariable::Assign(IMetaUnit^ metaUnit)
		{
			instance->assign(ConvertMetaUnit(metaUnit));
		}
	}
}
